package com.example.demo.service.impl;

import com.example.demo.constant.SystemConstant;
import com.example.demo.constant.TaskConstant;
import com.example.demo.dto.CommodityDTO;
import com.example.demo.dto.OrderListDTO;
import com.example.demo.mapper.StudentOrderMapper;
import com.example.demo.mapper.StudentPointsMapper;
import com.example.demo.mapper.entity.Commodity;
import com.example.demo.mapper.entity.Order;
import com.example.demo.service.StudentOrderService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lucy
 * @create 2024-02-20 17:50
 */
@Service
public class StudentOrderServiceImpl implements StudentOrderService {

    @Autowired
    private StudentOrderMapper orderMapper;

    @Autowired
    private StudentPointsMapper commodityMapper;


    @Override
    public void createOrder(Order order) {
        orderMapper.insertOrder(order);
    }

    @Override
    public PageVo<OrderListDTO> getStatusOrderList(PageBo pageBo,  int studentId, int status) {
        List<Order> orderList = orderMapper.selectStatusOrderList(studentId, status);
        List<OrderListDTO> OrderListDTOList = new ArrayList<>();
        for (Order order : orderList) {
            OrderListDTO dto = new OrderListDTO();
            dto.setOrderId(order.getOrderId());
            dto.setOrderNumber(order.getOrderNumber());
            dto.setPointsUsed(order.getPointsUsed());
            Commodity commodity = commodityMapper.selectCommodityById(order.getCommodityId());
            dto.setCommodityName(commodity.getCommodityName());
            dto.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + commodity.getImageUrl());
            OrderListDTOList.add(dto);
        }
        return new PageVo<>(pageBo, OrderListDTOList);
    }

    @Override
    public PageVo<OrderListDTO> getCanceledOrderList(PageBo pageBo, int studentId) {
        List<Order> orderList = orderMapper.selectCanceledOrderList(studentId);
        List<OrderListDTO> OrderListDTOList = new ArrayList<>();
        for (Order order : orderList) {
            OrderListDTO dto = new OrderListDTO();
            dto.setOrderId(order.getOrderId());
            dto.setOrderNumber(order.getOrderNumber());
            dto.setPointsUsed(order.getPointsUsed());
            Commodity commodity = commodityMapper.selectCommodityById(order.getCommodityId());
            dto.setCommodityName(commodity.getCommodityName());
            dto.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + commodity.getImageUrl());
            OrderListDTOList.add(dto);
        }
        return new PageVo<>(pageBo, OrderListDTOList);
    }

    @Override
    public Order selectOrderById(int orderId) {
        return orderMapper.selectOrderById(orderId);
    }

    @Override
    public void updateCancelInfo(int orderId, int cancelReason) {
        // 获取取消订单时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime cancelDateTime = LocalDateTime.now();
        String cancelDate = cancelDateTime.format(formatter);
        orderMapper.updateCancelInfo(orderId, cancelReason, cancelDate);
    }

    @Override
    public void updateCompleteInfo(int orderId) {
        int status = TaskConstant.ORDER_STATUS_COMPLETE;
        // 获取完成订单时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime completeDateTime = LocalDateTime.now();
        String completeDate = completeDateTime.format(formatter);
        orderMapper.updateCompleteInfo(orderId, status, completeDate);
    }
}
